package models

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/bson"
)

type RoomBasic struct {
	RoomId    string `bson:"room_id"`
	Number    string `bson:"number"`
	Name      string `bson:"name"`
	Info      string `bson:"info"`
	OwnerId   string `bson:"owner_id"`
	Icon      string `bson:"icon"`
	UserId    string `bson:"user_id"`
	CreatedAt int64  `bson:"created_at"`
	UpdateAt  int64  `bson:"updated_at"`
}

func (RoomBasic) CollectionName() string {
	return "room_basic"
}

func InsertOneRoomBasic(rb *RoomBasic) error {
	_, err := MongoDB.Collection(RoomBasic{}.CollectionName()).
		InsertOne(context.Background(), rb)
	return err
}
func DeleteRoomBasic(RoomId string) error {
	_, err := MongoDB.Collection(RoomBasic{}.CollectionName()).
		DeleteOne(context.Background(), bson.M{
			"identity": RoomId,
		})
	if err != nil {
		return nil
	}
	return nil
}

func FindOne() (*RoomBasic, error) {
	ur := &RoomBasic{}
	err := MongoDB.Collection(RoomBasic{}.CollectionName()).
		FindOne(context.Background(), bson.D{}).Decode(ur)
	if err != nil {
		fmt.Println("没有找到对应的房间")
		panic("停止操作")
	}
	return ur, err
}
